package cn.itcast.order.service.Impl;

import cn.itcast.order.RestTemplate.BeanFactory;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    @Qualifier("curTemplate")
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 通过静态工厂去拿

/*
        Order order =  orderMapper.findById(orderId);
        String url = "http://localhost:8081/user/" + order.getUserId();;
        order.setUser(BeanFactory.getRestTemplate().getForObject(url, User.class));
*/

        // 通过原生AutoWired去拿
        Order order =  orderMapper.findById(orderId);
        String url = "http://localhost:8081/user/" + order.getUserId();

        order.setUser(restTemplate.getForObject(url, User.class));


        return order;
    }
}
